package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CourseOrder;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CourseOrderQuery;
import cn.wolfcode.crm.service.ICourseOrderService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    private ICourseOrderService courseOrderServiceImpl;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CourseOrderQuery qo) {
        //根据 字典目录sn job 查询出对应的字典明细
        List<SystemDictionaryItem> subjects = systemDictionaryItemService.selectDicItemTitleByDicSn("subject");
        model.addAttribute("subjects", subjects);

        //查出所有客户 模态框列出
        List<Customer> customers = customerService.listAll();
        model.addAttribute("customers", customers);

        PageInfo<CourseOrder> pageInfo = courseOrderServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);
        return "/courseOrder/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        courseOrderServiceImpl.delete(id);
        return new JsonResult("删除成功", true);

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CourseOrder courseOrder, Long id) {
        if (id != null) {
            courseOrderServiceImpl.update(courseOrder);
        } else {
            courseOrderServiceImpl.save(courseOrder);
        }
        return new JsonResult("新增/编辑成功", true);
    }
}
